Put_Sprite_Mask:
; A = x coordinate
; L = y coordinate
; B = number of rows
; 
; IX = address of sprite
; Sprite_Width is the width in multiples of 8



Number_Of_Rows .equ appbackupscreen + 754

	push af
	ld a, b
	ld (Number_Of_Rows),a
	pop af

    LD     H, 0
    LD     D, H
    LD     E, L
    ADD    HL, HL
    ADD    HL, DE
    ADD    HL, HL
    ADD    HL, HL

    LD     E, A
    SRL    E
    SRL    E
    SRL    E
    ADD    HL, DE

    LD     DE, plotsscreen
    ADD    HL, DE
  
   ; LD     D, 0                 ; Correct graph buffer address
   ; LD     E, C                 ; if clipping the left side
   ; SBC    HL, DE 


 
    
    AND    7
    JR     Z, _Aligned

    LD     C, A
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL    
    
_RowLoop:

    PUSH   BC
    
    ;Stores width into B, which will be decreased 1 by 1 until one row is complete
    LD A, (Sprite_Width)

    LD B, A
    

_ColumnLoop:
    PUSH BC 
       
    LD     B, C
    LD     C, (IX)
    XOR    A
    PUSH DE
	push ix	
	call ldixmask 



    LD D, (IX)
	pop ix
    LD E, 0
_ShiftLoop:
    SRL    C
    RRA
    SRL D
    RR E
    DJNZ   _ShiftLoop
  

    INC    HL
    LD B, A
    LD A, E
    CPL 
    AND    (HL)
    OR B
    LD     (HL), A

    DEC    HL
    LD     A, D
    CPL 
    AND    (HL)
    OR C
    LD     (HL), A
    POP DE
    POP BC
    INC HL
    INC IX
    DJNZ _ColumnLoop
   
    ADD    HL, DE
   
    POP    BC
    DJNZ   _RowLoop
    
    RET

_Aligned:
   
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL  
     
_PutLoop:
    PUSH BC
    LD A, (Sprite_Width)
    LD B, A 

_PutLoop2:

	push ix	
	call ldixmask 

  
    LD     A, (IX)
	POP IX
    LD C, (IX)
    CPL
    AND    (HL)
    OR C
    LD     (HL), A
    INC  IX

    INC HL
    DJNZ _PutLoop2
_PutLoopAdd:
    POP BC
    
    ;ADD    HL, DE
    ;DJNZ _PutLoop 
 ADD HL, DE
 DJNZ _PutLoop
 RET 


ldixmask:

	push af
	push bc
	push de
	push hl


	

	ld a, (Number_Of_Rows)
	ld b,a
	ld a, (Sprite_Width)
	ld d,0
	ld e,a

ldixmask2:

	add ix, de
	djnz ldixmask2

	pop hl
	pop de
	pop bc
	pop af

	ret	

